package com.neo4j.neo4jController;

import com.neo4j.neo4jService.DeptService;
import com.neo4j.neo4jService.RelationShipService;
import com.neo4j.node.DeptEntity;
import com.neo4j.node.RelationShip;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("neo4j")
public class DeptController {
    @Autowired
    private DeptService deptService;

    @Autowired
    private RelationShipService relationShipService;

    /**
     * CEO
     *    -设计部
     *        - 设计1组
     *        - 设计2组
     *    -技术部
     *        - 前端技术部
     *        - 后端技术部
     *        - 测试技术部
     */
    @GetMapping("/create")
    public void create(){
        DeptEntity CEO = DeptEntity.builder().deptName("CEO").build();
        DeptEntity dept1 = DeptEntity.builder().deptName("设计部").build();
        DeptEntity dept11 = DeptEntity.builder().deptName("设计1组").build();
        DeptEntity dept12 = DeptEntity.builder().deptName("设计2组").build();

        DeptEntity dept2 = DeptEntity.builder().deptName("技术部").build();
        DeptEntity dept21 = DeptEntity.builder().deptName("前端技术部").build();
        DeptEntity dept22 = DeptEntity.builder().deptName("后端技术部").build();
        DeptEntity dept23 = DeptEntity.builder().deptName("测试技术部").build();
        List<DeptEntity> depts = new ArrayList<>(Arrays.asList(CEO,dept1,dept11,dept12,dept2,dept21,dept22,dept23));
        deptService.saveAll(depts);

        RelationShip relationShip1 = RelationShip.builder().parent(CEO).child(dept1).build();
        RelationShip relationShip2 = RelationShip.builder().parent(CEO).child(dept2).build();
        RelationShip relationShip3 = RelationShip.builder().parent(dept1).child(dept11).build();
        RelationShip relationShip4 = RelationShip.builder().parent(dept1).child(dept12).build();
        RelationShip relationShip5 = RelationShip.builder().parent(dept2).child(dept21).build();
        RelationShip relationShip6 = RelationShip.builder().parent(dept2).child(dept22).build();
        RelationShip relationShip7 = RelationShip.builder().parent(dept2).child(dept23).build();
        List<RelationShip> relationShips = new ArrayList<>(Arrays.asList(relationShip1,relationShip2,relationShip3,relationShip4,relationShip5
                ,relationShip6,relationShip7));
        relationShipService.saveAll(relationShips);
    }

    @GetMapping("deleteRelationShip")
    public void deleteRelationShip(Long id){
        relationShipService.deleteById(id);
    }

    @GetMapping("deleteDept")
    public void deleteDept(Long id){
        deptService.deleteById(id);
    }

    @GetMapping("deleteAll")
    public void deleteAll(){
        deptService.deleteAll();
        relationShipService.deleteAll();
    }
    @GetMapping("getByTitle")
    public List<DeptEntity> getByTitle(@RequestParam("title") String title){
        return deptService.findByTitle(title);
    }
}
